package com.zbkj.admin.task.mszs;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qiniu.util.StringUtils;
import com.zbkj.common.constants.MszsConstants;
import com.zbkj.common.model.mszs.MszsOrder;
import com.zbkj.common.model.mszs.MszsRoom;
import com.zbkj.common.utils.DateUtil;
import com.zbkj.service.service.mszs.MszsOrderService;
import com.zbkj.service.service.mszs.MszsRoomService;
import com.zbkj.service.service.mszs.QimoWebCallService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Optional;

/**
 * 民宿助手-自动分配房间task
 * @author lingfe
 */
@Slf4j
@Component
@Configuration //读取配置
@EnableScheduling // 2.开启定时任务
public class MszsAutoDistributionRoomTask {

    @Autowired
    private MszsOrderService mszsOrderService;

    @Autowired
    private MszsRoomService roomService;

    @Autowired
    private QimoWebCallService qimoWebCallService;

    //@Scheduled(cron = "0 */1 * * * ?") //每分钟执行一次
    @Scheduled(fixedDelay = 1000 * 20L) //20s-同步一次数据
    public void init(){
        log.info("---自动分配房间task: Execution Time - {}", DateUtil.nowDateTime());
        try{
            //定义查询酒店订单对象
            LambdaQueryWrapper<MszsOrder> orderQW=new LambdaQueryWrapper<>();
            orderQW.isNotNull(MszsOrder::getJdId);
            orderQW.eq(MszsOrder::getOrderStatusNumber, MszsConstants.MSZS_ORDER_STATUS_INT_Pending_check_in);//待入住

            //获取酒店订单信息
            List<MszsOrder> orderList = mszsOrderService.list(orderQW);
            orderList.forEach(order->{
                //1、分配房间,获取一条未入住的房间信息,并绑定订单同时更新房间状态
                MszsRoom mszsRoom = roomService.getWhereStatusOne(order.getJdId(),0);
                Optional.ofNullable(mszsRoom).ifPresent(room->{
                    //2、订单绑定房间,同时更新订单状态
                    order.setRoomId(room.getId());
                    order.setOrderStatus(MszsConstants.MSZS_ORDER_STATUS_STR_check_in);
                    order.setOrderStatusNumber(MszsConstants.MSZS_ORDER_STATUS_INT_check_in);
                    mszsOrderService.updateById(order);

                    //3、更新房间状态,更改为"已入住=2"
                    room.setStatus(MszsConstants.MSZS_ROOM_STATUS_INT_check_in);
                    roomService.updateById(room);

                    //4、验证-订单是否绑定用户号码
                    if(StringUtils.isNullOrEmpty(order.getUserPhone())){
                        log.info("订单：{}，未绑定用户手机号，不发送短信。",order.getOrderNo());
                    }else{
                        //5、发送短信-房间信息
                        qimoWebCallService.sendRoomInfoSms(order.getUserPhone(),room);
                    }
                });

                // TODO 如果没有空房间了怎么办？
            });
        }catch (Exception e){
            e.printStackTrace();
            log.error("---自动分配房间task-发生错误:" + " | msg : {}",e.getMessage());
        }
    }
}
